#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStandardItemModel>

#include "mypacketmodel.h"
#include <QDebug>
#include <QAction>
#include <deque>

std::deque<PPACKET> bufferDeque;

QList<MyPacket*> packetList;
MyPacketModel pModel(&packetList);

//////////////////////////////////////////////////////////////////////////

HANDLE hMutex;
bool stopThreads;

QAction *actionFilters[35];
//////////////////////////////////////////////////////////////////////////

extern HANDLE CompletionPort;
extern const char *PacketNames[];

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

	int i;

	for (i=0; i<35; ++i)
	{
		actionFilters[i] = new QAction(PacketNames[i], this);
		actionFilters[i]->setCheckable(true);
		actionFilters[i]->setChecked(true);
		connect(actionFilters[i], SIGNAL(triggered()), this, SLOT(on_actionFilter_clicked()));
		ui->menuFilter->addAction(actionFilters[i]);
	}

	connect(ui->actionStart, SIGNAL(triggered()), this, SLOT(on_startButton_clicked()));
	connect(ui->actionStop,  SIGNAL(triggered()), this, SLOT(on_stopButton_clicked()));

    connect(ui->tableView,  SIGNAL(doubleClicked(QModelIndex)), this, SLOT(on_tableView_doubleClicked(QModelIndex)));

    ui->tableView->setModel(&pModel);
    ui->tableView->setColumnWidth(0, 60);
    ui->tableView->setColumnWidth(1, 60);
    ui->tableView->setColumnWidth(2, ui->tableView->width()-140);

	packInfDlg = new PacketInformationDialog(this);

	hMutex = CreateMutex( NULL, FALSE, NULL);

	stopThreads = true;
}

MainWindow::~MainWindow()
{
    delete ui;
	delete packInfDlg;
}

void MainWindow::on_startButton_clicked()
{
    HANDLE hTh;
    DWORD dwThreadId_1, dwThreadId_2, dwWaitResult, i;

	dwWaitResult = WaitForSingleObject(hMutex, 5000L);
		stopThreads = false;
	ReleaseMutex(hMutex);

	for (i=0; i<35; ++i)
	{
		actionFilters[i]->setEnabled(false);
	}
	
	//for (i=0; i<10; ++i)
	{
		hTh = CreateThread(NULL, 0, WorkerThread, NULL, 0, &dwThreadId_1);
		if (hTh == NULL)
		{
			QMessageBox::critical(this, "CreateThread() failure",
				("Error code: " + GetLastError()));
			CloseHandle(hTh);        
			return;
		}
		SetThreadPriority(hTh, THREAD_PRIORITY_HIGHEST);
		CloseHandle(hTh);
	}
		

	hTh = CreateThread(NULL, 0, PacketGeneratorThread, NULL, 0, &dwThreadId_2);
	if (hTh == NULL)
	{
		QMessageBox::critical(this, "CreateThread() failure",
			("Error code: " + GetLastError()));
		CloseHandle(hTh);        
		return;
	}

//     ui->startButton->setEnabled(false);
//     ui->stopButton->setEnabled(true);
	ui->actionStart->setEnabled(false);
	ui->actionStop->setEnabled(true);

}

void MainWindow::on_stopButton_clicked()
{
	DWORD dwWaitResult, i;
    //PostQueuedCompletionStatus(CompletionPort, 0, NULL, NULL);

	dwWaitResult = WaitForSingleObject(hMutex, 5000L);
		stopThreads = true;
	ReleaseMutex(hMutex);

//     ui->startButton->setEnabled(true);
//     ui->stopButton->setEnabled(false);
	ui->actionStart->setEnabled(true);
	ui->actionStop->setEnabled(false);

	for (i=0; i<35; ++i)
	{
		actionFilters[i]->setEnabled(true);
	}
}

void MainWindow::on_tableView_doubleClicked(QModelIndex index)
{
    QString text;
    MyPacket *pPac = packetList[index.row()];

	text = QString::fromStdWString(pPac->GetDetailedInfo());

    packInfDlg->SetHTML(text);
    packInfDlg->show();
}

void MainWindow::on_actionFilter_clicked()
{
	
}